/**
 * create by young (young2f@outlook.com) on 2020/9/12
 */


'use strict';
import React from 'react';
import {connect} from 'dva/index';
import {DatePicker, Form, Select, Table} from 'antd';

import styles from '../../index.less';
import FormBase from '../../../components/Base/FormBase';
import moment from 'moment';
import 'moment/locale/zh-cn';
import ViewTitle from "../../../components/ViewTitle";
import {FormField, ModalView, SearchForm} from "antd-extra";
import MaterialElement from "../../finance/modal/MaterialElement";
import ModalCreateDiscardRecord from "../modal/ModalCreateDiscardRecord";

const {RangePicker} = DatePicker;
moment.locale('zh-cn');

const _ = require('lodash');
const {Option} = Select;
const table = 'SalesOrder';
const title = '订单';

@Form.create()
@connect(({
            user,
            global,
            customer,
            sales,
            dict,
            material,
            store
          }) => ({...user, ...global, ...dict, ...material, ...customer, ...sales, ...store}))
export default class TabsInboundLots extends FormBase {
  constructor(props) {
    super(props);
    this.state = {};
  }

  componentDidMount() {
    const payload = this.props.form.getFieldsValue();
    _.map(payload, (value, key) => {
      if (value === '') {
        payload[key] = undefined;
      }
    });
    // if (!payload.projectType) payload["projectType"] = {$ne: "租用"};
    payload.key = 'inboundDate';
    this.props.dispatch({
      type: 'store/findInboundLots',
      payload
    });
  }

  columns() {
    const {callback} = this.props;
    return [
      {
        title: '物料名称', dataIndex: 'name', key: 'name', render: (value, record) => {
          const {code, name} = record;
          return <MaterialElement code={code} name={name}></MaterialElement>;
        }
      },

      {title: '物料形态', dataIndex: 'subcategory', key: 'subcategory'},
      {title: '物料规格', dataIndex: 'spec', key: 'spec'},
      {title: '物料类型', dataIndex: 'type', key: 'type'},
      {title: '物料编号', dataIndex: 'number', key: 'number'},
      {title: '单价', dataIndex: 'price', key: 'price'},
      {title: '采购数量', dataIndex: 'count', key: 'count'},
      {title: '入库数量', dataIndex: 'inboundCount', key: 'inboundCount'},
      {title: '占用数量', dataIndex: 'usedCount', key: 'usedCount'},
      {title: '报废数量', dataIndex: 'discardCount', key: 'discardCount'},
      {title: '供应商', dataIndex: 'vendor', key: 'vendor'},
      {title: '采购公司', dataIndex: 'division', key: 'division'},
      {
        title: '入库日期', dataIndex: 'inboundDate', key: 'inboundDate', render: value => {
          console.log(value);
          return <div>{moment(value).format('YYYY-MM-DD HH:mm:ss')}</div>;
        }
      },
      {
        title: '操作', dataIndex: 'opt', key: 'opt', fixed: 'right', render: (value, record) => {
          return <a style={{marginRight: '6px'}} onClick={() => {
            ModalView.open(ModalCreateDiscardRecord, {
              title,
              width: '1080px'
            }, {
              record,
              callback: payload => {
                this.props.dispatch({
                  type: 'store/createDiscardRecord',
                  payload
                }).then(res => {
                  // callback(res);
                  this.requestReturn(res);
                  this.componentDidMount();
                });
              }
            });
          }}>添加报废记录</a>;
        }
      }
    ];
  }

  render() {
    const {form, discards = [], inboundLots = []} = this.props;
    console.log('inboundLots', inboundLots);
    return <div>
      <ViewTitle
        title={`入库批次列表`}
        level={1}
      ></ViewTitle>
      <SearchForm
        style={{marginLeft: '6px', marginRight: '6px'}}
        onSearch={() => {
          this.state.current = 1;
          this.componentDidMount();
        }}
        onReset={this.handleReset}
        cols={4}
        min={8}
        expandText="展开"
        form={form}
      >
        {[
          // <FormField
          //   label="供应商名称"
          //   name="vendor"
          //   key={1}
          // >
          //   <Input allowClear placeholder={`请输入`}/>
          // </FormField>,
          // <FormField
          //   label={'采购公司'}
          //   name={`division`}
          //   key={`division`}
          // >
          //   <Select allowClear placeholder={`请输入采购公司`}>{this.getDivision(divisionDict)}</Select>
          // </FormField>,
          <FormField
            label={`创建时间`}
            name="createdTs"
            key={3}
            initialValue={[this.getSearchStart1Months(), this.getDefaultSearchEndDate()]}
          >
            <RangePicker allowClear={true}
              // style={{ width: '150%' }}
            />
          </FormField>
        ]}
      </SearchForm>
      <Table
        className={styles.antTable}
        bordered={true}
        columns={this.columns()}
        dataSource={inboundLots}
        scroll={{x: true}}
        pagination={{
          current: this.state.current,
          total: this.state.total,
          showSizeChanger: true,
          onChange: page => {
            this.state.current = page;
            this.componentDidMount();
          },
          onShowSizeChange: (current, pageSize) => {
            this.state.current = 1;
            this.state.pageSize = pageSize;
            this.componentDidMount();
          }
        }}
      ></Table>
    </div>;
  }
}
